package norbert.flickr.api {
	import norbert.flickr.io.FlickrService;
	import mx.rpc.events.ResultEvent;
	import mx.logging.ILogger;
	import mx.logging.Log;
	
	public class Auth extends FlickrService {
		private static var logger: ILogger = Log.getLogger("norbert.flickr.api.Auth");
		
		/** Permission to read private information */
		public static const PERMISSION_READ:String = "read";
		
		/** Permission to add, edit and delete photo metadata (includes READ) */
		public static const PERMISSION_WRITE:String = "write";
		
		/** Permission to delete photos (includes WRITE and READ) */
		public static const PERMISSION_DELETE:String = "delete";
		
		/** No permission to do anything ( not authenticated ) */
		public static const PERMISSION_NONE:String = "none";
		
		/* PROPERTIES */
		
		[Inspectable(category="General", enumeration="checkToken,getFrob,getFullToken,getToken")]
		override public function set method(value: String): void {
			super.method = value;
		}
		
		/* CONSTRUCTOR */
		
		public function Auth() {
			methodBase = "flickr.auth";
			
			addEventListener(ResultEvent.RESULT, onResult);
		}
		
		/* EVENT HANDLERS */
		
		private function onResult(event: ResultEvent): void {
			if (method == "getToken") {
				flickr.permission = event.result.auth.perms;
				flickr.token = event.result.auth.token;
				flickr.user = event.result.auth.user.@username;
				
				logger.debug("{0}: permission: {1}, token: {2}", this, flickr.permission, flickr.token);
			}
		}
	}
}
